package com.zxgj.datadirect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxgj.datadirect.entity.DataDirectApply;
import com.zxgj.datadirect.mapper.DataDirectApplyMapper;
import com.zxgj.datadirect.service.IDataDirectApplyService;
import com.zxgj.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 数据直达基层-申请记录Service业务层处理
 * 
 * @author ruoyi
 */
@Slf4j
@Service
public class DataDirectApplyServiceImpl extends ServiceImpl<DataDirectApplyMapper, DataDirectApply>
        implements IDataDirectApplyService {

    /**
     * 查询数据直达基层-申请记录
     * 
     * @param id 数据直达基层-申请记录主键
     * @return 数据直达基层-申请记录
     */
    @Override
    public DataDirectApply selectDataDirectApplyById(Long id) {
        return baseMapper.selectDataDirectApplyById(id);
    }

    /**
     * 查询数据直达基层-申请记录列表
     * 
     * @param dataDirectApply 数据直达基层-申请记录
     * @return 数据直达基层-申请记录
     */
    @Override
    public List<DataDirectApply> selectDataDirectApplyList(DataDirectApply dataDirectApply) {
        // 直接调用Mapper的自定义方法，而不是使用MyBatis-Plus的通用方法
        return baseMapper.selectDataDirectApplyList(dataDirectApply);
    }

    /**
     * 新增数据直达基层-申请记录
     * 
     * @param dataDirectApply 数据直达基层-申请记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDataDirectApply(DataDirectApply dataDirectApply) {
        // 设置默认值
        if (dataDirectApply.getApplyTime() == null) {
            dataDirectApply.setApplyTime(new Date());
        }
        if (StringUtils.isEmpty(dataDirectApply.getStatus())) {
            dataDirectApply.setStatus("PENDING");
        }
        if (StringUtils.isEmpty(dataDirectApply.getCreateBy())) {
            dataDirectApply.setCreateBy("admin");
        }
        if (dataDirectApply.getCreateTime() == null) {
            dataDirectApply.setCreateTime(new Date());
        }
        
        // 直接调用Mapper的自定义方法
        return baseMapper.insertDataDirectApply(dataDirectApply);
    }

    /**
     * 修改数据直达基层-申请记录
     * 
     * @param dataDirectApply 数据直达基层-申请记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDataDirectApply(DataDirectApply dataDirectApply) {
        // 设置更新时间
        dataDirectApply.setUpdateTime(new Date());
        if (StringUtils.isEmpty(dataDirectApply.getUpdateBy())) {
            dataDirectApply.setUpdateBy("admin");
        }
        
        // 直接调用Mapper的自定义方法
        return baseMapper.updateDataDirectApply(dataDirectApply);
    }

    /**
     * 批量删除数据直达基层-申请记录
     * 
     * @param ids 需要删除的数据直达基层-申请记录主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDataDirectApplyByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        
        return baseMapper.deleteDataDirectApplyByIds(ids);
    }

    /**
     * 删除数据直达基层-申请记录信息
     * 
     * @param id 数据直达基层-申请记录主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDataDirectApplyById(Long id) {
        return baseMapper.deleteDataDirectApplyById(id);
    }

    /**
     * 分页查询申请记录
     * 
     * @param page 分页参数
     * @param dataDirectApply 查询条件
     * @return 分页结果
     */
    public Page<DataDirectApply> getApplyPage(Page<DataDirectApply> page, DataDirectApply dataDirectApply) {
        QueryWrapper<DataDirectApply> wrapper = new QueryWrapper<>();
        
        // 设置查询条件
        if (dataDirectApply != null) {
            if (StringUtils.isNotEmpty(dataDirectApply.getApplyNo())) {
                wrapper.like("apply_no", dataDirectApply.getApplyNo());
            }
            if (StringUtils.isNotEmpty(dataDirectApply.getApplicant())) {
                wrapper.like("applicant", dataDirectApply.getApplicant());
            }
            if (StringUtils.isNotEmpty(dataDirectApply.getStatus())) {
                wrapper.eq("status", dataDirectApply.getStatus());
            }
            if (StringUtils.isNotEmpty(dataDirectApply.getApplyType())) {
                wrapper.eq("apply_type", dataDirectApply.getApplyType());
            }
        }
        
        wrapper.orderByDesc("create_time");
        return this.page(page, wrapper);
    }
}
